# 基本配置
TARGET      := unit_test_v1
BUILD_DIR   := ./build
SRC_DIR     := ./src
INC_DIRS    := ./inc

# 编译模式：可选值 cross(交叉编译) 或 native(本地编译)
COMPILE_MODE ?= cross

# 芯片平台：可选值 rk3588, rk3568
SOC_PLATFORM ?= rk3568

# 根据编译模式和芯片平台设置工具链和内核路径
ifeq ($(COMPILE_MODE), cross)
    $(info Building for $(SOC_PLATFORM) using cross-compilation)
    ifeq ($(SOC_PLATFORM), rk3588)
        CROSS_COMPILE   ?= /media/test/sde/ubuntu2204/rk3588/tl3588-evm_buildroot-2021.11_v2.2/rk3588_linux_release/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
        CFLAGS          += -DTARGET_RK3588
    else ifeq ($(SOC_PLATFORM), rk3568)
        CROSS_COMPILE  	?= /media/test/sde/ubuntu2204/rk3568/tl3568f-evm_v1.5/rk356x_linux_release_v1.3.1/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-
        CFLAGS          += -DTARGET_RK3568
    else
        $(error Unsupported SOC_PLATFORM for cross compilation: $(SOC_PLATFORM). Supported values: rk3588, rk3568)
    endif
else ifeq ($(COMPILE_MODE), native)
    $(info Building natively for $(SOC_PLATFORM))
    CROSS_COMPILE       ?=
    ifeq ($(SOC_PLATFORM), rk3588)
        CFLAGS          += -DTARGET_RK3588
    else ifeq ($(SOC_PLATFORM), rk3568)
        CFLAGS          += -DTARGET_RK3568
    else
        $(error Unsupported SOC_PLATFORM for native compilation: $(SOC_PLATFORM). Supported values: rk3588, rk3568)
    endif
else
    $(error Unsupported COMPILE_MODE: $(COMPILE_MODE). Supported values: cross, native)
endif

CC                      := $(CROSS_COMPILE)gcc
STRIP                   := $(CROSS_COMPILE)strip

# 库选项, null 或库名称
LIB_NAME ?= pcie_mcdev
ifneq ($(LIB_NAME), null)
    # 第三方库配置 - 使用绝对路径
    LIB_DIR   := $(abspath ../39_pcie_cdev_lib)
    LIB_INC   := $(LIB_DIR)/inc
    LIB_FILE  := $(LIB_DIR)/build/lib$(LIB_NAME).so
    
    # 编译选项
    CFLAGS      += -Wall -Wextra -I$(INC_DIRS) -I$(LIB_INC)
    
    # 更健壮的库链接选项
    ifeq ($(suffix $(LIB_FILE)), .a)
        # 静态库链接
        LDFLAGS     := -lm -lpthread $(LIB_FILE)
    else
        # 动态库链接
       LDFLAGS     := -lm -lpthread -L$(dir $(LIB_FILE)) -l$(LIB_NAME)
        # 设置运行时库路径（推荐）
       LDFLAGS     += -Wl,-rpath,$(dir $(LIB_FILE))
    endif
else
    # 编译选项（不链接第三方库）
    CFLAGS      += -Wall -Wextra -I$(INC_DIRS)
    LDFLAGS     := -lm -lpthread
endif

# 工具命令
RM          := rm -rf
MKDIR       := mkdir -p

# 自动查找源文件
SRCS        := $(shell find $(SRC_DIR) -name '*.c' 2>/dev/null)
OBJS        := $(SRCS:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)

# 默认目标
all: $(BUILD_DIR)/$(TARGET)

# 检查库文件是否存在（作为依赖）
ifneq ($(LIB_NAME), null)
$(BUILD_DIR)/$(TARGET): | check_library
endif

# 主目标链接
$(BUILD_DIR)/$(TARGET): $(OBJS) | $(BUILD_DIR)
	$(CC) $(OBJS) -o $@ $(LDFLAGS)
	@echo "=== 应用程序构建完成 ==="

# 编译源文件
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
	@$(MKDIR) $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

# 创建构建目录
$(BUILD_DIR):
	$(MKDIR) $@

# 检查库文件
check_library:
ifneq ($(LIB_NAME), null)
    ifneq ($(wildcard $(LIB_FILE)),)
		@echo "找到库文件: $(LIB_FILE)"
    else
		$(warning 库文件不存在: $(LIB_FILE))
		@echo "请先构建库项目或检查路径"
		@echo "预期路径: $(LIB_FILE)"
		@false
    endif
endif

# 清理
clean:
	$(RM) $(BUILD_DIR)
	@echo "=== 清理完成 ==="

LOG_LEVEL ?= 5
# 发布版本
release: CFLAGS += -DNDEBUG -DLOG_LEVEL=$(LOG_LEVEL)
release: all
	$(STRIP) --strip-debug $(BUILD_DIR)/$(TARGET)
	@echo "=== 发布版本构建完成 ==="

# 调试版本
debug: CFLAGS += -g -DDEBUG -DLOG_LEVEL=$(LOG_LEVEL)
debug: all
	@echo "=== 调试版本构建完成 ==="

# 显示构建信息
info:
	@echo "=== 构建信息 ==="
	@echo "目标: $(TARGET)"
	@echo "编译器: $(CC)"
	@echo "源文件数量: $(words $(SRCS))"
	@echo "源文件: $(SRCS)"
	@echo "交叉编译工具链: $(if $(CROSS_COMPILE),$(CROSS_COMPILE),使用系统gcc)"
ifneq ($(LIB_NAME), null)
	@echo "第三方库头文件: $(LIB_INC)"
	@echo "第三方库文件: $(LIB_FILE)"
	@echo "库文件存在: $(if $(wildcard $(LIB_FILE)),是,否)"
	@echo "链接选项: $(LDFLAGS)"
endif
	@echo "编译选项: $(CFLAGS)"
	@echo "================="

# 显示帮助信息
help:
	@echo "可用目标:"
	@echo "  all     - 构建应用程序（默认）"
	@echo "  clean   - 清理构建文件"
	@echo "  release - 构建发布版本（优化+strip）"
	@echo "  debug   - 构建调试版本（调试信息）"
	@echo "  info    - 显示构建信息"
	@echo "  help    - 显示此帮助信息"
	@echo ""
	@echo "环境变量:"
	@echo "  COMPILE_MODE=cross|native   # 编译模式（默认: cross）"
	@echo "  SOC_PLATFORM=rk3568|rk3588  # 芯片平台（默认: rk3568）"
	@echo "  LIB_NAME=libname|null       # 库名称（默认: libpcie_mcdev.so）"
	@echo "  LOG_LEVEL=level            # 调试日志级别（默认: 5）"
	@echo ""
	@echo "示例:"
	@echo "  make                        # 默认交叉编译"
	@echo "  make COMPILE_MODE=native    # 本地编译"
	@echo "  make debug                  # 调试版本"
	@echo "  make clean                  # 清理"

.PHONY: all clean info help release debug check_library